package com.kl.forum.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.CircleCommentLightMapper;
import com.kl.forum.dao.CircleCommentMapper;
import com.kl.forum.dao.CircleReplyMapper;
import com.kl.forum.entity.ArticleComment;
import com.kl.forum.entity.CircleComment;
import com.kl.forum.entity.CircleCommentLight;
import com.kl.forum.entity.CircleReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CircleReplyService {
    @Autowired
    private CircleReplyMapper circleReplyMapper;
    @Autowired
    private CircleCommentMapper circleCommentMapper;
    @Autowired
    private CircleCommentLightMapper circleCommentLightMapper;
    public CircleReply insert(Integer commentId,Integer userId,String content,Date date,String userName,String commentName,Integer commentNameId,Integer articleId){
        CircleReply circleReply=new CircleReply();
        circleReply.setCommentId(commentId);
        circleReply.setCreateTime(date);
        circleReply.setUserId(userId);
        circleReply.setIsDel(1);
        circleReply.setContent(content);
        circleReply.setUserName(userName);
        circleReply.setCommentName(commentName);
        circleReply.setCommentNameId(commentNameId);
        circleReply.setArticleId(articleId);
        circleReplyMapper.insertSelective(circleReply);
        return circleReply;
    }

    public PageInfo findCircleCommentReply(Integer commentId, Integer userId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("commentId",commentId);
        map.put("userId",userId);
        List<Map> list=circleReplyMapper.findCircleCommentReply(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }
    public int delCircleCommentReply(Integer id){
        return  circleReplyMapper.deleteByPrimaryKey(id);
    }
    public CircleReply findOneCircleReply(Integer id){
        return  circleReplyMapper.selectByPrimaryKey(id);
    }
    public  int update(CircleReply circleReply){
        return  circleReplyMapper.updateByPrimaryKeySelective(circleReply);
    }

    public CircleComment findArticleCommentReplyList(Integer userId, Integer commentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map map1=new HashMap();
        map1.put("commentId",commentId);
        CircleComment circleComment=circleCommentMapper.findOneComment(map1);
        if (userId==null){
            circleComment.setLightStatus(0);
        }
        CircleCommentLight light= circleCommentLightMapper.findOneCircleCommentLight(userId,commentId);
        if(light!=null){
            circleComment.setLightStatus(light.getStatus());
        }
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("commentId",commentId);
        List<Map> list=circleReplyMapper.findArticleCommentReplyList(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        circleComment.setPageInfoReply(pageInfo);
        return circleComment;
    }
}
